// crc.js
export class CRC32 {
    constructor() {
        this.crc = -1; // Initialize CRC with 0xFFFFFFFF
        this.table = new Uint32Array(256);

        // Build CRC32 lookup table
        for (let i = 0; i < 256; i++) {
            let c = i;
            for (let j = 0; j < 8; j++) {
                c = (c & 1) ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
            }
            this.table[i] = c;
        }
    }

    reset() {
        this.crc = -1;
    }

    update(bytes) {
        for (let byte of bytes) {
            this.crc = (this.crc >>> 8) ^ this.table[(this.crc ^ byte) & 0xff];
        }
    }

    getValue() {
        return (this.crc ^ -1) >>> 0; // XOR with -1 and bitwise NOT to get the final CRC32 value
    }

    getResult() {
        return this.getValue().toString(16).padStart(8, '0');
    }
}
